"""
title:商品浏览页面
author:陈诚
date:2022/1/6
"""
from Common.Base import Base, open_browser
import time

goods_list_url = 'http://localhost:8080/ecshop/category.php?id=16'


class BrowserGoods(Base):
    """封装表现层,制作定位器"""
    goods_list = ('css selector', '.goods-title>a')   # 定位商品

    """封装操作层,操作元素"""

    def get_goods_list(self):
        """
        获取商品标题
        :return: title
        """
        goods = self.find_elements(self.goods_list)  # 定位所有商品元素
        return [i.get_attribute('title') for i in goods]

    def click_all_goods(self):
        """点击所有商品"""
        goods_titles = self.get_goods_list()
        for i in goods_titles:
            goods_loc = ('css selector', f'a[title="{i}"]')  # 针对单个商品编写对应的定位器
            self.click(locator=goods_loc)
            time.sleep(1)
            self.back_browser()


if __name__ == '__main__':
    goods = BrowserGoods(open_browser())
    goods.open_url(goods_list_url)
    goods.click_all_goods()
    goods.close_browser()